package targetToOffer.nintehDay;

import java.util.Arrays;
import java.util.LinkedList;

/**
 * @author Godc
 * @description
 */
public class SpiralOrder {
    public static void main(String[] args) {
        SpiralOrderSolution s = new SpiralOrderSolution();
        int[] ints = s.spiralOrder(new int[][]{{1, 2, 3,4}, {5,6,7,8}, {9,10,11,12}});
        System.out.println(Arrays.toString(ints));
    }
}
class SpiralOrderSolution {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length==0) return new int[0];
        int l = 0,r = matrix[0].length-1,t=0,b=matrix.length-1;
        int[] res = new int[(r+1)*(b+1)];
        int k = 0;
        while(true){
            for(int i = l;i<=r;i++) res[k++] = matrix[t][i];
            if(++t>b) break;
            for(int i = t;i<=b;i++) res[k++] = matrix[i][r];
            if(--r<l) break;
            for(int i = r;i>=l;i--) res[k++] = matrix[b][i];
            if(--b<t) break;
            for(int i = b;i>=t;i--) res[k++] = matrix[i][l];
            if(++l>r) break;
        }
        return res;
    }
}